

_realname=llama.cpp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
epoch=1
pkgver=b5481
pkgrel=1
pkgdesc="Library and tools for running inference with Meta's LLaMA model (and derivatives) in C/C++ (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64' 'mingw64')
url="https://github.com/ggml-org/llama.cpp"
license=('spdx:MIT')
depends=(
  "${MINGW_PACKAGE_PREFIX}-curl"
  "${MINGW_PACKAGE_PREFIX}-cc-libs"
  "${MINGW_PACKAGE_PREFIX}-ggml"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
)
source=("https://github.com/ggml-org/llama.cpp/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('50461623349f33fd77620d9ac4eb1ccfb58d5ea733b0d0ca298d2ce260d07e66')

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DLLAMA_BUILD_EXAMPLES=ON \
      -DLLAMA_ALL_WARNINGS=OFF \
      -DLLAMA_BUILD_TESTS=OFF \
      -DLLAMA_CURL=ON \
      -DLLAMA_USE_SYSTEM_GGML=ON \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      ../${_realname}-${pkgver}

  cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" cmake --install .

  # Rename output files by prefixing them with llama (if not already prefixed and if not DLL)
  for i in "${pkgdir}"${MINGW_PREFIX}/bin/*.{exe,py}; do
    filename=$(basename "$i")
    if [[ ! ("$filename" =~ ^llama) ]]; then
      mv "${i}" "${pkgdir}${MINGW_PREFIX}/bin/llama-${filename}"
    fi
  done

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
